package com.ruoyi.system.utils;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.exception.UncertifiedException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GwEnterpriseInfo;
import com.ruoyi.system.mapper.GwEnterpriseInfoMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.tencent.essbasic.api.CreateConsoleLoginUrl;
import com.tencent.essbasic.config.Config;
import com.tencentcloudapi.essbasic.v20210526.models.Agent;
import com.tencentcloudapi.essbasic.v20210526.models.CreateConsoleLoginUrlResponse;
import com.tencentcloudapi.essbasic.v20210526.models.UserInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class AgentUtil {

    @Resource
    private GwEnterpriseInfoMapper gwEnterpriseInfoMapper;
    @Resource
    private SysUserMapper sysUserMapper;


    /**
     * 设置Agent
     *
     * @return Agent
     */
    public Agent setAgent() {
        Agent agent = new Agent();
        UserInfo userInfo = new UserInfo();
        agent.setAppId(Config.AppId);
        agent.setProxyAppId(Config.ProxyAppId);

        Map<String, String> map = new HashMap<String, String>();
        //获取两个openid
        GwEnterpriseInfo gwEnterpriseInfo = gwEnterpriseInfoMapper.selectGwEnterpriseInfoById(SecurityUtils.getLoginUser().getEnterpriseId());

        if (ObjectUtils.isEmpty(gwEnterpriseInfo)) {
            throw new GlobalException("请登录绑定企业的用户");
        }

        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());

        //修改成获取当前公司的 ProxyOrganizationOpenId 与 ProxyOperatorOpenId
        agent.setProxyOrganizationOpenId(gwEnterpriseInfo.getSignOpenId());
        userInfo.setOpenId(sysUser.getSignOpenId());
        agent.setProxyOperator(userInfo);

        //判断企业  是否认证
        if (!"1".equals(gwEnterpriseInfo.getCertificationStatus())) {
            //主账号未认证
            if ("1".equals(sysUser.getUserType())) {
                //账号未认证
                CreateConsoleLoginUrlResponse pc = CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "");
                CreateConsoleLoginUrlResponse mini = CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "APP");
                map.put("pc", pc.getConsoleUrl());
                map.put("mini", mini.getConsoleUrl());
                throw new UncertifiedException("企业账号未认证", HttpStatus.Uncertified, map);
            } else {
                throw new GlobalException("企业主账号未认证操作失败!");
            }
        } else {
            if (!"1".equals(sysUser.getCertificationStatus())) {
                //主账号未认证
                if ("1".equals(sysUser.getUserType())) {
                    //账号未认证
                    CreateConsoleLoginUrlResponse pc = CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "");
                    CreateConsoleLoginUrlResponse mini = CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "APP");
                    map.put("pc", pc.getConsoleUrl());
                    map.put("mini", mini.getConsoleUrl());
                    throw new UncertifiedException("企业账号未认证", HttpStatus.Uncertified, map);
                } else {
                    //判断主账号认证没
                    SysUser user = sysUserMapper.selectUserByPhone(gwEnterpriseInfo.getPhone());
                    if (ObjectUtils.isNotEmpty(user)) {
                        if ("1".equals(user.getCertificationStatus())) {
                            //账号未认证
                            CreateConsoleLoginUrlResponse pc = CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "");
                            CreateConsoleLoginUrlResponse mini = CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "APP");
                            map.put("pc", pc.getConsoleUrl());
                            map.put("mini", mini.getConsoleUrl());
                            throw new UncertifiedException("账号未认证", HttpStatus.Uncertified, map);
                        } else {
                            throw new GlobalException("企业主账号未认证操作失败!");
                        }
                    } else {
                        throw new GlobalException("企业主账号不存在操作失败!");
                    }
                }
            }
        }
        return agent;
    }

    public Map<String, String> setACAgent() {
        Agent agent = new Agent();
        UserInfo userInfo = new UserInfo();
        agent.setAppId(Config.AppId);
        agent.setProxyAppId(Config.ProxyAppId);

        //获取两个openid
        GwEnterpriseInfo gwEnterpriseInfo = gwEnterpriseInfoMapper.selectGwEnterpriseInfoById(SecurityUtils.getLoginUser().getEnterpriseId());

        if (ObjectUtils.isEmpty(gwEnterpriseInfo)) {
            throw new GlobalException("请登录绑定企业的用户");
        }

        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());

        //修改成获取当前公司的 ProxyOrganizationOpenId 与 ProxyOperatorOpenId
        agent.setProxyOrganizationOpenId(gwEnterpriseInfo.getSignOpenId());
        userInfo.setOpenId(sysUser.getSignOpenId());
        agent.setProxyOperator(userInfo);

        //判断企业  是否认证
        if (!"1".equals(gwEnterpriseInfo.getCertificationStatus())) {
            //主账号未认证
            if (!"1".equals(sysUser.getUserType())) {
                throw new GlobalException("企业主账号未认证操作失败!");
            }
        } else {
            if (!"1".equals(sysUser.getCertificationStatus())) {
                //主账号未认证
                if (!"1".equals(sysUser.getUserType())) {
                    //判断主账号认证没
                    SysUser user = sysUserMapper.selectUserByPhone(gwEnterpriseInfo.getPhone());
                    if (ObjectUtils.isNotEmpty(user)) {
                        if (!"1".equals(user.getCertificationStatus())) {
                            throw new GlobalException("企业主账号未认证操作失败!");
                        }
                    } else {
                        throw new GlobalException("企业主账号不存在操作失败!");
                    }
                }
            }
        }

        Map<String, String> map = new HashMap<>();
        CreateConsoleLoginUrlResponse pc =
                CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), null);

        CreateConsoleLoginUrlResponse mini =
                CreateConsoleLoginUrl.createConsoleLoginUrl(agent, gwEnterpriseInfo.getEnterpriseName(), "APP");

        map.put("pc", pc.getConsoleUrl());
        map.put("mini", mini.getConsoleUrl());

        return map;
    }

}
